from flet import (
	Control,
	Column,
	Container,
	Row,
	Text,
	NavigationRail,
	NavigationRailDestination,
	alignment,
	border_radius,
	colors,
	icons,
	padding,
	margin,
)
import itertools




class Sidebar(Control):
	id_counter = itertools.count()
	
	def __init__(self, app_layout, page):
		super().__init__()
		self.app_layout = app_layout
		self.page = page
		self.top_nav_items = [
			NavigationRailDestination(
				label_content=Text("看板"),
				label="看板",
				icon=icons.BOOK_OUTLINED,
				selected_icon=icons.BOOK_OUTLINED
			),
			NavigationRailDestination(
				label_content=Text("成员"),
				label="成员",
				icon=icons.PERSON,
				selected_icon=icons.PERSON
			),
		
		]
		self.top_nav_rail = NavigationRail(
			selected_index=None,
			label_type="all",
			on_change=self.top_nav_change,
			destinations=self.top_nav_items,
			bgcolor=colors.BLUE_GREY,
			extended=True,
			expand=True
		)
		self.id = next(Sidebar.id_counter)
	
	def build(self):
		self.view = Container(
			content=Column([
				Row([
					Text("工作区"),
				]),
				# 分隔线
				Container(
					bgcolor=colors.BLACK26,
					border_radius=border_radius.all(30),
					height=1,
					alignment=alignment.center_right,
					width=220
				),
				self.top_nav_rail,
				# 分隔线
				Container(
					bgcolor=colors.BLACK26,
					border_radius=border_radius.all(30),
					height=1,
					alignment=alignment.center_right,
					width=220
				),
			], tight=True),
			padding=padding.all(15),
			margin=margin.all(0),
			width=250,
			bgcolor=colors.BLUE_GREY,
		)
		return self.view
	
	def top_nav_change(self, e):
		self.top_nav_rail.selected_index = e.control.selected_index
		self.update()
		



class Sidebar(Container):
	def __init__(self, parent, page, *args, **kwargs):
		super().__init__(*args, **kwargs)
		self.parent = parent
		self.page = page
		self.nav_list = List(color=colors.GREY_100)
		self.nav_items = [
			ListItem(
				controls=[
					Icon(icons.HOME),
					Text("Home")
				],
				on_click=self.go_home
			),
			ListItem(
				controls=[
					Icon(icons.PERSON),
					Text("Profile")
				],
				on_click=self.go_profile
			),
			ListItem(
				controls=[
					Icon(icons.NOTIFICATIONS),
					Text("Notifications")
				],
				on_click=self.go_notifications
			),
			ListItem(
				controls=[
					Icon(icons.SETTINGS),
					Text("Settings")
				],
				on_click=self.go_settings
			)
		]
		self.nav_list.controls = self.nav_items
		self.controls = [self.nav_list]

	def go_home(self, e):
		self.parent.active_view = self.create_home_view()

	def go_profile(self, e):
		self.parent.active_view = self.create_profile_view()

	def go_notifications(self, e):
		self.parent.active_view = self.create_notifications_view()

	def go_settings(self, e):
		self.parent.active_view = self.create_settings_view()

	def create_home_view(self):
		# Create the control for home view
		return Container(
			controls=[
				Text("Welcome to the Home View")
			],
			alignment="center",
			horizontal_alignment="center"
		)

	def create_profile_view(self):
		# Create the control for profile view
		return Container(
			controls=[
				Text("Welcome to the Profile View")
			],
			alignment="center",
			horizontal_alignment="center"
		)

	def create_notifications_view(self):
		# Create the control for notifications view
		return Container(
			controls=[
				Text("Welcome to the Notifications View")
			],
			alignment="center",
			horizontal_alignment="center"
		)

	def create_settings_view(self):
		# Create the control for settings view
		return Container(
			controls=[
				Text("Welcome to the Settings View")
			],
			alignment="center",
			horizontal_alignment="center"
		)
